/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player;

import com.cmp.core.Main;
import com.cmp.core.constants.CollisionGroupsConstants;
import com.cmp.player.controls.BulletControl;
import com.cmp.player.controls.CollisionControl;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * Create colliding bullets with this.
 * @author krummer
 */
public class BulletFactory {
    public static void makeBullet(Node bullet) {
        bullet.setUserData("isBullet", true);
        
        //add Model
        AssetManager assets = Main.app.getAssetManager();
        bullet.attachChild(assets.loadModel("Models/sphere.j3o"));
        
        
        BoxCollisionShape shape = new BoxCollisionShape(bullet.getLocalScale());
        RigidBodyControl body = new RigidBodyControl(shape);
        GhostControl ghostBody = new GhostControl(shape);
        ghostBody.setCollisionGroup(CollisionGroupsConstants.BULLET);
        ghostBody.setCollideWithGroups(CollisionGroupsConstants.MAP | CollisionGroupsConstants.PLAYER);
        bullet.addControl(body);
        bullet.addControl(ghostBody);
        
        class BCollisionControl extends CollisionControl {
            private Node bullet;
            
            public BCollisionControl(Node bullet) {
                this.bullet = bullet;
            }

            @Override
            public void onCollision(PhysicsCollisionEvent event, Spatial other, boolean isA) {
                this.bullet.getControl(BulletControl.class).setCollisionNode((Node) other);
            }

            public Control cloneForSpatial(Spatial spatial) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            
        }
        
        bullet.addControl(new BulletControl());
        bullet.addControl(new BCollisionControl(bullet));
        
        
    }
        
}
